const BASE_URL="http://api-breakingnews-web.itheima.net"
// 1.安全访问
let token=localStorage.getItem("token")
if(!token){
    location.href="/login.html"
}
// 2.点击退出按钮
$("#logout").on("click",function(){
    layer.confirm('是否要退出?主人', { icon: 3, title: '提示' }, function (index) {
        //do something
    
        // token删除
        localStorage.removeItem('token')
    
        location.href = '/login.html'
    
        layer.close(index)
      })
})
// 获取个人信息
function mg_getUserInfo(){
    axios
    .get(`${BASE_URL}/my/userinfo`,{
        headers:{
            Authorization:token,
        },
    })
    .then(res=>{
        // 1.5 判断 token 失效的问题
        if(res.data.status===1&&res.data.message==="身份认证失败！"){
            localStorage.removeItem("token")
            location.href='/login.html'
            return
        }
        console.log("获取个人信息",res.data)
        let {user_pic,nickname,username}=res.data.data
        if(nickname===""){
            nickname=username
        }
        
        $(".nickname").html(nickname)
        //3.2 展示头像
        if(user_pic===null){
            let firstLetter=username[0].toUpperCase()
            $('.avatar').html(firstLetter)
        $('.avatar').css('display', 'inline-block')
        }else{
            $('.layui-nav-img').attr('src', user_pic)
            $('.layui-nav-img').css("display","inline-block")
        }


    })
}
mg_getUserInfo()
